using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceTools
{
    public partial class NewDatabaseForm : Form
    {
        private bool m_bMustTestConnection;
        private DBConnectionLib.Database m_Database;

        public DBConnectionLib.Database Database
        {
            get { return m_Database; }
            set { m_Database = value; }
        }

        public NewDatabaseForm(bool bMustTestConnection)
        {
            InitializeComponent();
            this.m_bMustTestConnection = bMustTestConnection;
        }

        private void NewDatabaseForm_Load(object sender, EventArgs e)
        {
            this.m_comboBoxAuthentication.SelectedIndex = 0;
            EnableButtons(0);
            this.btnOk.Enabled = !m_bMustTestConnection;
        }

        private void m_comboBoxAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableButtons(this.m_comboBoxAuthentication.SelectedIndex);
        }

        private void EnableButtons(int selIdx)
        {
            if (selIdx == 0)
            {
                this.m_textBoxPassword.Enabled = false;
                this.m_textBoxUserName.Enabled = false;
            }
            else
            {
                this.m_textBoxPassword.Enabled = true;
                this.m_textBoxUserName.Enabled = true;
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (CheckData())
            {
                DBConnectionLib.Database oDatabase = new DBConnectionLib.Database(
                    this.m_textBoxDatabaseName.Text, this.m_textBoxServerName.Text, this.m_textBoxDatabaseName.Text, "");

                if (this.m_comboBoxAuthentication.SelectedIndex == 1)
                {
                    oDatabase.UserName = this.m_textBoxUserName.Text;
                    oDatabase.Password = this.m_textBoxPassword.Text;
                }

                if (oDatabase.TestSmoConnection())
                {
                    MessageBox.Show("Connection succeeded.");
                    this.m_Database = null;
                    this.m_Database = oDatabase;
                    this.btnOk.Enabled = true;
                }
                else
                {
                    DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                        new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm("Connection failed.\n" + oDatabase.ErrorMessage);
                    oError.Show();

                    //MessageBox.Show("Connection failed.\n" + oDatabase.ErrorMessage );
                }
                
            }
        }

        private bool CheckData()
        {
            if (String.IsNullOrEmpty(this.m_textBoxServerName.Text))
            {
                MessageBox.Show("Please specify a server name.");
                return false;
            }
            if (String.IsNullOrEmpty(this.m_textBoxDatabaseName.Text))
            {
                MessageBox.Show("Please specify a database name.");
                return false;
            }
            return true;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (CheckData())
            {
                DBConnectionLib.Database oDatabase = new DBConnectionLib.Database(
                    this.m_textBoxDatabaseName.Text, this.m_textBoxServerName.Text, this.m_textBoxDatabaseName.Text, "");

                if (this.m_comboBoxAuthentication.SelectedIndex == 1)
                {
                    oDatabase.UserName = this.m_textBoxUserName.Text;
                    oDatabase.Password = this.m_textBoxPassword.Text;
                }
                this.m_Database = null;
                this.m_Database = oDatabase;
                this.DialogResult = DialogResult.OK;
                this.Close();

            }

        }

    }
}